<template>
  <div class="mainsu">
    <div class="main_spac1">
      <div class="main_white padding10 paddingBottom20">
        <div class="mainor_form">
          <div class="mainor_te">
            <router-link to="/financial/salesman/flowing" class="mainor_tea">
              差额流水
            </router-link>
            <el-button @click="export_de">导出</el-button>
          </div>
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="70px"
          >
            <el-form-item label="商户名称" prop="name">
              <el-input
                v-model.trim="form.name"
                placeholder="商户名称"
                clearable
                class="wid220"
              ></el-input>
            </el-form-item>
            <el-form-item label="收货人" prop="receive_man">
              <el-input
                v-model.trim="form.receive_man"
                placeholder="收货人"
                clearable
                class="wid220"
              ></el-input>
            </el-form-item>
            <el-form-item label="收货手机" prop="mobile">
              <el-input
                v-model.trim="form.mobile"
                placeholder="收货手机"
                clearable
                class="wid220"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button
                class="reset marginLeft10"
                type="primary"
                @click="$refs['userListFrom'].resetFields()"
                >重置</el-button
              >
              <el-button
                type="primary"
                @click="getArticleList"
                :loading="listLoading"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div class="main_num boxdq marginTop10">
          <div class="main_num_l box">
            <p>
              应退金额：
              <em>{{ list.loss }}</em>
            </p>
            <p>
              欠收金额：
              <em>{{ list.profit }}</em>
            </p>
          </div>
        </div>
        <div class="main_table marginTop10" style="min-height: 550px">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list.list.data"
            element-loading-text="加载中"
            fit
            border
            highlight-current-row
          >
            <el-table-column
              label="商户名称"
              prop="company_name.company_name"
            />
            <el-table-column label="手机号" prop="company_name.mobile" />
            <el-table-column label="联系人" prop="company_name.name" />
            <el-table-column
              label="订单差额"
              prop="balance"
              :render-header="renderHeader_purchase"
            />
            <el-table-column
              label="管理操作"
              align="center"
              fixed="right"
              width="140"
              class-name="main_hei"
            >
              <template slot-scope="scope">
                <el-link type="warning" @click="knotClick(scope.$index)"
                  >结差额</el-link
                >
                <router-link
                  :to="
                    '/financial/salesman/flowing?name=' +
                    scope.row.company_name.company_name
                  "
                >
                  <el-link type="success">记录</el-link>
                </router-link>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <el-pagination
              :current-page="current.page"
              :page-sizes="[20, 30, 50, 100]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  balance_index,
  export_balance_list,
} from "@/api/financial/financialSalesman";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";

export default {
  components: {},
  data() {
    return {
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 列表数据
      list: {
        list: [],
      },
      listLoading: false,
      // 搜索
      form: {
        receive_man: "",
        name: "",
        mobile: "",
      },
    };
  },
  created() {
    // 列表
    this.balance_index();
  },
  methods: {
    // 结差额
    knotClick(ind) {
      this.$confirm(
        "差额为负数时，需商户在线支付差额，完成差额清零！",
        "提示",
        {
          confirmButtonText: "确定",
          type: "warning",
        }
      )
        .then(() => {})
        .catch(() => {});
    },
    // 导出明细
    export_de() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name
      );
      export_balance_list(data)
        .then((res) => {
          const link = document.createElement("a");
          let blob = new Blob([res], { type: "application/vnd.ms-excel" });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          link.download = "商户差额";
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        })
        .catch(() => {
          this.$message.error("导出失败");
        });
    },
    // 订单差额 ？注释
    renderHeader_purchase(h, { column, $index }) {
      // 这里在最外层插入一个div标签
      return h("div", [
        // h即为cerateElement的简写
        // 在div里面插入span
        h("span", {
          // 表示内容
          domProps: {
            innerHTML: column.label,
          },
          on: {
            click: () => {
              console.log(`${column.label}   ${$index}`);
            },
          },
        }),
        h(
          "el-tooltip",
          {
            // 表示属性
            attrs: {
              effect: "dark",
              content: "注释：由于商品单价的波动，订单金额多退少补产生的差额！",
              placement: "top",
            },
          },
          [
            h("i", {
              style: "margin-left: 5px;margin-top:-1px;",
              class: "el-icon-question table-msg",
            }),
          ]
        ),
      ]);
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.balance_index();
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.balance_index();
    },
    changePage(page) {
      this.current.page = page;
      this.balance_index();
    },
    // 列表
    balance_index() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, this.list_form(), this.current)
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current
      );
      balance_index(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));

      return form;
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
  .mainor_form form {
    width: calc(100% - 160px);
  }
}
</style>
